package com.turman.plyopengl

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Create
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.turman.plyopengl.ui.theme.PlyOpenGLTheme

class MainActivity : ComponentActivity() {
    private var lifecycleState by mutableStateOf(Lifecycle.Event.ON_ANY)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onPause(owner: LifecycleOwner) {
                super.onPause(owner)
                lifecycleState = Lifecycle.Event.ON_PAUSE
            }

            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                lifecycleState = Lifecycle.Event.ON_RESUME
            }
        })

        setContent {
            var showDraw = remember {
                mutableStateOf(false)
            }
            PlyOpenGLTheme {
                Box(modifier = Modifier.fillMaxSize()) {
                    Row(modifier = Modifier.fillMaxWidth()) {
                        Surface(modifier = if (showDraw.value) Modifier.weight(0.5f) else Modifier) {
                            AndroidView(factory = {
                                BaseGLSurfaceView(context = this@MainActivity)
                            }, update = {
                                when (lifecycleState) {
                                    Lifecycle.Event.ON_PAUSE -> it.onPause()
                                    Lifecycle.Event.ON_RESUME -> it.onResume()
                                    else -> {}
                                }
                            })
                        }
                        Surface(modifier = Modifier.weight(0.5f)) {
                            AnimatedVisibility(visible = showDraw.value) {
                                AndroidView(factory = {
                                    DrawGLSurfaceView(context = this@MainActivity)
                                }, update = {
                                    when (lifecycleState) {
                                        Lifecycle.Event.ON_PAUSE -> it.onPause()
                                        Lifecycle.Event.ON_RESUME -> it.onResume()
                                        else -> {}
                                    }
                                }, modifier = Modifier.weight(0.5f))
                            }
                        }
                    }
                    Column(modifier = Modifier.align(Alignment.TopEnd)) {
                        Button(onClick = {
                            showDraw.value = !showDraw.value
                        }) {
                            Icon(imageVector = Icons.Default.Create, contentDescription = "")
                        }
                    }
                }
            }
        }
    }
}